const getTimeStr = (timestamp) => {
  const time = new Date(timestamp);
  const now = new Date();
  const s = parseInt((now - time) / 1000);

  let dateTime = new Date(timestamp);
  let y = dateTime.getFullYear();
  let m = dateTime.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  let d = dateTime.getDate();
  d = d < 10 ? "0" + d : d;
  return y + "-" + m + "-" + d;
};

const formatDate = (timestamp) => {
  var time =
    String(timestamp).length === 10
      ? new Date(parseInt(timestamp) * 1000)
      : new Date(parseInt(timestamp));
  var y = time.getFullYear(); // 年
  var m = time.getMonth() + 1; // 月
  if (m < 10) {
    m = "0" + m;
  }
  var d = time.getDate(); // 日
  if (d < 10) {
    d = "0" + d;
  }
  var h = time.getHours(); // 时
  if (h < 10) {
    h = "0" + h;
  }
  var mm = time.getMinutes(); // 分
  if (mm < 10) {
    mm = "0" + mm;
  }
  var s = time.getSeconds(); // 秒
  if (s < 10) {
    s = "0" + s;
  }
  var timeStr = y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
  return timeStr;
};

const checkTime = (begintime, endtime, flag = false) => {
  if (!endtime) return true;

  if (flag) {
    begintime = getTimeStr(begintime);
    endtime = `${endtime.slice(0, 4)}-${endtime.slice(4, 6)}-${endtime.slice(
      6,
      8
    )}`;
  } else {
    endtime = getTimeStr(endtime);
    begintime = `${begintime.slice(0, 4)}-${begintime.slice(
      4,
      6
    )}-${begintime.slice(6, 8)}`;
  }
  if (!begintime) return true;

  var time1 = new Date(begintime).getTime();
  var time2 = new Date(endtime).getTime();
  if (begintime == "") {
    return false;
  }
  if (endtime == "") {
    return false;
  }
  if (time1 > time2) {
    return false;
  }

  //判断时间跨度是否大于3个月
  var arr1 = begintime.split("-");
  var arr2 = endtime.split("-");
  arr1[1] = parseInt(arr1[1]);
  arr1[2] = parseInt(arr1[2]);
  arr2[1] = parseInt(arr2[1]);
  arr2[2] = parseInt(arr2[2]);
  var flag = true;
  if (arr1[0] == arr2[0]) {
    //同年
    if (arr2[1] - arr1[1] > 3) {
      //月间隔超过3个月
      flag = false;
    } else if (arr2[1] - arr1[1] == 3) {
      //月相隔3个月，比较日
      if (arr2[2] > arr1[2]) {
        //结束日期的日大于开始日期的日
        flag = false;
      }
    }
  } else {
    //不同年
    if (arr2[0] - arr1[0] > 1) {
      flag = false;
    } else if (arr2[0] - arr1[0] == 1) {
      if (arr1[1] < 10) {
        //开始年的月份小于10时，不需要跨年
        flag = false;
      } else if (arr1[1] + 3 - arr2[1] < 12) {
        //月相隔大于3个月
        flag = false;
      } else if (arr1[1] + 3 - arr2[1] == 12) {
        //月相隔3个月，比较日
        if (arr2[2] > arr1[2]) {
          //结束日期的日大于开始日期的日
          flag = false;
        }
      }
    }
  }
  if (!flag) {
    // 超过三个月
    return false;
  }
  return true;
};

const judgeDateRange = (time1Str, time2Str, m) => {
  time1Str = formatDate(time1Str);
  time2Str = `${time2Str.slice(0, 4)}-${time2Str.slice(4, 6)}-${time2Str.slice(
    6,
    8
  )}`;
  if (!time1Str || !time2Str) return false;
  const time1 = new Date(time1Str);
  const time2 = new Date(time2Str);
  const year1 = time1.getFullYear();
  const year2 = time2.getFullYear();
  const month1 = time1.getMonth() + 1;
  const month2 = time2.getMonth() + 1;
  const day1 = time1.getDate();
  const day2 = time2.getDate();
  if (year2 === year1) {
    // 判断两个日期 是否是同年
    if (month2 - month1 > m) {
      // 相差是否 在m个月中
      return false;
    } else if (month2 - month1 === m) {
      // 如果正好为 m月 判断天数
      if (day2 > day1) {
        return false;
      }
    }
  } else {
    // 不同年
    if (year2 - year1 > 1) {
      return false;
    } else if (year2 - year1 === 1) {
      if (month2 > m || month1 + m - month2 < 12) {
        return false;
      } else if (month1 + m - month2 === 12) {
        // 正好为m月 判断天数
        if (day2 > day1) {
          return false;
        }
      }
    }
  }
  return true;
};

export { checkTime, judgeDateRange };
